package com.ossjk.oa.paid.controller;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.paid.entity.Paid;
import com.ossjk.oa.paid.service.IPaidService;
import com.ossjk.oa.system.entity.Employee;
import com.ossjk.oa.system.entity.Post;
import com.ossjk.oa.system.service.IDictionaryService;
import com.ossjk.oa.system.service.IEmployeeService;
import com.ossjk.oa.system.service.IPostService;
import com.ossjk.payment.util.PaymentUtil;

/**
 * <p>
 * 薪酬管理 前端控制器
 * </p>
 *
 * @author Jason
 * @since 2019-01-14
 */
@RestController
@RequestMapping("/paid/addPaid")
public class AddPaidController extends BaseController {
	@Autowired
	private IEmployeeService iEmployeeService;
	@Autowired
	private IPostService iPostService;
	@Autowired
	private IDictionaryService iDictionaryService;
	@Autowired
	private IPaidService iPaidService;

	@RequiresPermissions("00450000")
	@GetMapping("/list")
	public Object list(String did, String dateStr) {
		Map map = new HashMap<>();
		Wrapper wrapper = Condition.create();
		if (!CommonUtil.isBlank(did)) {
			wrapper.eq("d.id", did);
		}
		// 根据部门查找员工
		List<Employee> employees = iEmployeeService.selectVoList(wrapper);
		map.put("employees", employees);
		// 查找每个员工的薪酬记录
		List<Paid> paids = new ArrayList<>();
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
			Date date = simpleDateFormat.parse(dateStr);
			for (Employee employee : employees) {
				String eid = employee.getId();
				Wrapper wrapper2 = Condition.create().eq("eid", eid).eq("date", date).eq("isdelete", "1");
				Paid paid = iPaidService.selectOne(wrapper2);
				// 有记录
				if (!CommonUtil.isBlank(paid)) {
					paids.add(paid);
				}
			}
			map.put("paids", paids);
			if (!CommonUtil.isBlank(did)) {
				Wrapper wrapper2 = Condition.create().eq("did", did);
				// 根据部门查找岗位
				map.put("posts", iPostService.selectList(wrapper2));
			}
			// 前端倒序增加所以搜索时根据倒序查询
			Wrapper wrapper3 = Condition.create().like("type", "2").orderBy("crtm", false);
			map.put("dictionaries", iDictionaryService.selectList(wrapper3));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			employees = null;
			paids = null;
		}
		return responseSuccess(map);
	}

	@RequiresPermissions("004500000000")
	@PostMapping("/preview")
	public Object preview(String str) {
		List list = (List) JSONArray.parse(str);
		List data = new ArrayList<>();
		// 遍历每个员工
		for (Object obj : list) {
			Map<String, String> varMap = new HashMap<String, String>();
			Map<String, String> sysMap = new HashMap<String, String>();
			String id = "";
			String pid = "";
			List list2 = (List) obj;
			// 遍历员工的每一项薪酬组成
			for (Object obj2 : list2) {
				Map map = (Map) obj2;
				// 常量
				if (CommonUtil.isEquals(map.get("type"), "1")) {
					// 变量
				} else if (CommonUtil.isEquals(map.get("type"), "2")) {
					varMap.put((String) map.get("key"), (String) map.get("value"));
					// 系统常量
				} else if (CommonUtil.isEquals(map.get("type"), "3")) {
					sysMap.put((String) map.get("key"), (String) map.get("value"));
				} else {
					if (CommonUtil.isEquals(map.get("key"), "id")) {
						id = (String) map.get("value");
					} else if (CommonUtil.isEquals(map.get("key"), "pid")) {
						pid = (String) map.get("value");
					}
				}
			}
			String expressionsJson = "";
			String formulaJson = "";
			Post post = iPostService.selectById(pid);
			expressionsJson = post.getExpression();
			formulaJson = post.getFormula();
			PaymentUtil paymentUtil = new PaymentUtil();
			double salary = 0;
			Map data2 = new HashMap<>();
			try {
				salary = paymentUtil.handlePayment(expressionsJson, formulaJson, varMap, sysMap);
				expressionsJson = paymentUtil.getExpressionsJson();
				formulaJson = paymentUtil.getFinalFormula();
				data2.put("id", id);
				data2.put("salary", salary);
				data2.put("expressionsJson", expressionsJson);
				data2.put("formulaJson", formulaJson);
				data.add(data2);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				list2 = null;
				data2 = null;
				varMap = null;
				sysMap = null;
			}
		}
		list = null;
		return responseSuccess(data);
	}

	@RequiresPermissions("004500000005")
	@PostMapping("/saveSalary")
	public Object saveSalary(String str, String dateStr) {
		List list = (List) JSONArray.parse(str);
		List<Paid> paids = new ArrayList<>();
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
			Date date = simpleDateFormat.parse(dateStr);
			for (Object obj : list) {
				Map map = (Map) obj;
				Paid paid = new Paid();
				paid.setFormula((String) map.get("formulaJson"));
				paid.setExpression((String) map.get("expressionsJson"));
				paid.setEid((String) map.get("id"));
				paid.setSalary((BigDecimal) map.get("salary"));
				paid.setDate(date);
				paids.add(paid);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			list = null;
		}

		if (iPaidService.insertBatchPaids(paids)) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}
}
